'use strict';

module.exports = app => {
  const { STRING, INTEGER, DECIMAL } = app.Sequelize;

  const OrderItem = app.model.define('order_item', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    orderId: { type: INTEGER, allowNull: false, comment: '订单ID' },
    productId: { type: INTEGER, allowNull: false, comment: '商品ID' },
    quantity: { type: INTEGER, allowNull: false, comment: '购买数量' },
    price: { type: DECIMAL(10, 2), allowNull: false, comment: '商品单价' },
    amount: { type: DECIMAL(10, 2), allowNull: false, comment: '小计金额' },
    productName: { type: STRING(100), allowNull: false, comment: '商品名称' },
    productImage: { type: STRING(255), comment: '商品图片' },
  });

  OrderItem.associate = function() {
    app.model.OrderItem.belongsTo(app.model.Order, { foreignKey: 'orderId', as: 'order' });
    app.model.OrderItem.belongsTo(app.model.Product, { foreignKey: 'productId', as: 'product' });
  };

  return OrderItem;
};
